package org.example.BlogCases;


import org.example.InitAndEnd;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.By;


import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.stream.Stream;

import static java.lang.Thread.sleep;

@TestMethodOrder(MethodOrderer.class)
public class Cases extends InitAndEnd {

    Date date = new Date();


    public static Stream<Arguments> Generator(){
        return Stream.of(Arguments.arguments("http://127.0.0.1:8080/blog_content.html","博客正文","自动化测试"));
    }
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "/LoginSuccess.csv")
    public void LoginSuccess(String username,String password,String blog_list_url) throws InterruptedException {
        //打开登录页面
        webDriver.get("http://127.0.0.1:8080/blog.html");
        TimeWait.timewait();
        //填写用户名,密码
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        TimeWait.timewait();
        //点击提交按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        TimeWait.timewait();
        //获取当前页面的url
        String cur_url = webDriver.getCurrentUrl();
        TimeWait.timewait();
        //比较url
        Assertions.assertEquals(blog_list_url,cur_url);
    }
    @Order(2)
    @Test
    public void BlogList(){
        webDriver.get("http://127.0.0.1:8080/myblog_list.html");
        TimeWait.timewait();
        int tileNum = webDriver.findElements(By.cssSelector("#artlist > div > div.title")).size();
        System.out.println(tileNum);

        Assertions.assertNotEquals(0,tileNum);

    }

    @Order(5)
    @ParameterizedTest
    @MethodSource("Generator")
    public void BlogDetail(String expectedUrl,String expectedTitle,String expectedBlogTitle){
        TimeWait.timewait();

        webDriver.findElement(By.cssSelector("#artlist > div > a:nth-child(4)")).click();
        TimeWait.timewait();
        String curUrl = webDriver.getCurrentUrl();
        if(curUrl.contains(expectedUrl)){
            System.out.println("测试通过");
        }else {
            System.out.println("测试未通过");
            return;
        }
        TimeWait.timewait();
        String curTitle = webDriver.getTitle();
        Assertions.assertEquals(expectedTitle,curTitle);
        TimeWait.timewait();
        String curBlogTitle = webDriver.findElement(By.cssSelector("#title")).getText();
        Assertions.assertEquals(expectedBlogTitle,curBlogTitle);

    }
    @Order(3)
    @Test
    public void WriteBlog() throws InterruptedException {
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        TimeWait.timewait();
        webDriver.findElement(By.cssSelector("#title")).sendKeys("自动化测试");
        TimeWait.timewait();
        webDriver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button")).click();
        TimeWait.timewait();
        webDriver.switchTo().alert().dismiss();
        String curUrl = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://127.0.0.1:8080/myblog_list.html",curUrl);

    }
    @Order(4)
    @Test
    public void BilgInfoChecked(){
        webDriver.get("http://127.0.0.1:8080/myblog_list.html");
        String firstBlogTitle = webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > div.title")).getText();
        String firstBlogTime = webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > div.date")).getText();
        Assertions.assertEquals(webDriver.findElement(By.cssSelector("#artlist > div > div.title")).getText(),firstBlogTitle);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");


        if(firstBlogTime.contains(formatter.format(date))){
            System.out.println("测试通过");

        }else{
            System.out.println("测试不通过");;
        }
    }
    @Order(6)
    @Test
    public void DeleteBlog() throws InterruptedException {
        webDriver.get("http://127.0.0.1:8080/myblog_list.html");
        TimeWait.timewait();
        int beforeNum = webDriver.findElements(By.cssSelector("#artlist > div > div.title")).size();

        webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > a:nth-child(6)")).click();
        sleep(3000);
        webDriver.switchTo().alert().accept();
        TimeWait.timewait();
        webDriver.switchTo().alert().accept();
        TimeWait.timewait();
        int AfterNum = webDriver.findElements(By.cssSelector("#artlist > div > div.title")).size();
        Assertions.assertNotEquals(AfterNum,beforeNum);
    }
}
